package cn.wygandwdn.common.message;

import cn.wygandwdn.common.enumeration.ResponseInfo;

/**
 * @description:
 * @author: WYG
 * @time: 2021/10/24 20:19
 */

public class RpcResponse<T> extends Message {

    /**
     * 响应状态码
     */
    private Integer code;
    /**
     * 响应消息
     */
    private String message;
    /**
     * 响应数据
     */
    private T data;
    /**
     * 异常信息
     */
    private Exception exception;

    public RpcResponse() {}

    public RpcResponse(long sequenceId, ResponseInfo info, T data, Exception exception) {
        super.setSequenceId(sequenceId);
        this.code = info.getCode();
        this.message = info.getMessage();
        this.data = data;
        this.exception = exception;
    }

    public static <T> RpcResponse<T> success(long sequenceId, T data) {
        RpcResponse<T> response = new RpcResponse<>();
        response.setSequenceId(sequenceId);
        response.setCode(ResponseInfo.HANDLE_SUCCESS.getCode());
        response.setMessage(ResponseInfo.HANDLE_SUCCESS.getMessage());
        response.setData(data);
        response.setException(null);
        return response;
    }

    public static <T> RpcResponse<T> fail(long sequenceId, Exception e) {
        RpcResponse<T> response = new RpcResponse<>();
        response.setSequenceId(sequenceId);
        response.setCode(ResponseInfo.HANDLE_FAIL.getCode());
        response.setMessage(ResponseInfo.HANDLE_FAIL.getMessage());
        response.setData(null);
        response.setException(e);
        return response;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Exception getException() {
        return exception;
    }

    public void setException(Exception exception) {
        this.exception = exception;
    }

    @Override
    public int getMessageType() {
        return RpcResponseMessage;
    }

    @Override
    public String toString() {
        return "RpcResponse{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                ", exception=" + exception +
                '}';
    }
}
